package agency.web.app.service.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



import agency.web.app.domain.Tour;
import agency.web.app.domain.Trip;
import agency.web.app.domain.User;
import agency.web.app.repository.TripDao;
import agency.web.app.repository.UserDao;
import agency.web.app.service.TripService;

@Service
@Transactional
public class TripServiceImpl implements TripService{

	@Autowired
	private TripDao tripRepository;

	public void addTrip(Trip t) {
		tripRepository.add(t);
		
	}

	public void modifyTrip(Trip t) {
		tripRepository.modify(t);
		
	}

	public List<Trip> findAllTrips() {
		return tripRepository.findAll();
	}

	public Trip findTripById(int id) {
		return tripRepository.findById(id);
	}

	public List<Trip> findTripByClient(User c) {
		Criterion criterion = Restrictions.eq("userId", c.getUserId());
		return tripRepository.findByCriteria(criterion);
	}

	public List<Trip> findTripByAgent(User a) {
		Criterion criterion = Restrictions.eq("userId", a.getUserId());
		return tripRepository.findByCriteria(criterion);
	}

	public List<Trip> findTripByDate(Date from, Date to) {
		Date[] date= {from,to};
		Criterion criterion = Restrictions.in("date", date);
		return tripRepository.findByCriteria(criterion);
	}

	public List<Trip> findTripByTour(Tour t) {
		Criterion criterion = Restrictions.eq("tourId", t.getTourId());
		return tripRepository.findByCriteria(criterion);
	}

	public void deleteTripById(int id) {
		tripRepository.delete(id);
		
	}

	public void deleteTrip(Trip t) {
		tripRepository.delete(t);
	}


}
